/**
 * Created by yanxiaoyao on 2017/03/03.
 */

'use.strict';
define(["app", "service/service", "directive/selectCarDir/selectCarDir"], function(app) {
    app.register.controller('selectCar', ['$scope', '$state', '$stateParams', 'commonService', '$location', 'OEMIDTRUE', '$rootScope', function($scope, $state, $stateParams, commonService, $location, OEMIDTRUE, $rootScope) {
        var partsId;
        $rootScope.spinner = true;
        //      console.log($('#partsId').val())
        //      初始化页面判断是否有partsId
        if ($('#partsId').val()) {
            partsId = $('#partsId').val();
//          console.log(partsId)
        } else if ($location.search().txtoemId) {
            partsId = "";
        } else {
            base.showMsg('请选择车型', function() {}, '');
        }


        //默认加载oem列表
        //console.log($stateParams.txtoemId)
        //如果有OEMcode根据oem加载
        if ($stateParams.txtoemId && $stateParams.txtoemId != 0) {
        	
        	var paramtrue = {
	                  "oemCode": $location.search().txtoemId,
//	                    "partsId": $location.search().partsId,
	                }
	            var paramstrue = JSON.stringify(paramtrue) ;
	            console.log(paramstrue)
				commonService.oemcodetrue(paramstrue, function(data) {
	                    //              console.log(data)
	                    $scope.dataoemcode=data.data
	                   console.log(data.data)
	                });
        	
            $rootScope.spinner = true;
            commonService.classify(function(data) {
                if (data.code == 0) {

                    $scope.classify = data.data
                    $('#partsIsecondtrue').val($location.search().threeId);
                }
            })
            var param = {
                "oemCode": $stateParams.txtoemId
            }
            var params = JSON.stringify(param);
            $scope.current = "";
            $scope.currentt = "";
            commonService.searchOEM(params, function(data) {
                //console.log(data)
                if (data.code == 0) {

                    $scope.oemlist = data.data;
                    //如果是再次添加默认勾选
                    if (localStorage.getItem('OEMARRTRUE')) {
                        OEMARR = localStorage.getItem('OEMARRTRUE').split(",");
                        $scope.ifSelect = function(id) {
                            return OEMARR.indexOf(id) != -1;
                        };
                    } else {
                        OEMARR = [];
                    }
                }

            });
        } else {
            commonService.classify(function(data) {
                if (data.code == 0) {
                    $scope.classify = data.data;
//                  console.log(data.data)
                    $scope.current = $location.search().oneId;
                    $scope.currentt = $location.search().threeId;
                    $scope.currenttwo = $location.search().twoId;
                    $scope.ShowcurrentTwo = "显示所有二级分类";
                    $('#partsIsecondtrue').val($location.search().threeId);
                    if ($location.search().twoId) {
                        $scope.showTwo = true;
                    } else {
                        $scope.showTwo = false;
                    }
                }
            })
//			
            //  判断是否存在三级分类id
            if ($location.search().threeId) {
//          	判断三级分类是否置灰
	//			commonService.oemcodetrue(function(data) {
	//              if (data.code == 0) {
	//                  console.log(data)
	//              }
	//          })
				var paramtrue = {
	//                  "assortId": $location.search().threeId,
	                    "partsId": $location.search().partsId,
	                }
	            var paramstrue = JSON.stringify(paramtrue) ;
//	            console.log(paramstrue)
				commonService.oemcodetrue(paramstrue, function(data) {
	                    //              console.log(data)
	                    $scope.dataoemcode=data.data
//	                   console.log(data)
	                });
                var param = {
                    "assortId": $location.search().threeId,
                    "partsId": partsId,
                }
                var params = JSON.stringify(param)
                commonService.LoadList(params, function(data) {
                    //              console.log(data)
                    $scope.oemlist = data.data;
                    if (localStorage.getItem('OEMARRTRUE')) {
                        OEMARR = localStorage.getItem('OEMARRTRUE').split(",");
                        $scope.ifSelect = function(id) {
                            return OEMARR.indexOf(id) != -1;
                        };
                    } else {
                        OEMARR = [];
                    }
                });
            }

        }

        //全选或全不选
        var OEMARR = []; //点击存入数组；
        var OEMARRTrue = []; //查看询价单数组
        $scope.dataOEM = false; //默认未选中
        $scope.checkAll = function(ifSelected, oemlist) {
            //console.log(oemlist)
            if (ifSelected == true) {
                $scope.ifSelect = function() {
                    return true;
                };
                for (var i = 0; i < oemlist.length; i++) {
                    OEMARR.push(oemlist[i].oemId);
                }
            } else {
                $scope.ifSelect = function() {
                    return false;
                };
                OEMARR = [];
            }
        }

        //复选框
        $scope.check = function(data) {
            // console.log(data)
            if (data.dataOEM == true) {
                OEMARR.push(data.oemId);
            } else {
                var index = OEMARR.indexOf(data.oemId);
                OEMARR.splice(index, 1)
            }
            // console.log(OEMARR)
        }

        //加入询价单
        $scope.SheetJion = function() {
            if (OEMARR != "") {
                base.showMsg('加入询价单成功', function() {
                    selsctfunction.inquirySheetfn(OEMARR, OEMARRTrue);
                    console.log(OEMARRTrue)
                }, '');
            } else {
                base.showMsg('请选择oem信息', function() {}, '');
            }
        }

        //查看询价单
        $scope.inquirySheet = function(currentt, current) {

            if ($('#partsId').val()) {
                partsId = $('#partsId').val();
//              console.log(partsId)
            } else if ($location.search().txtoemId) {
                partsId = "";
            } else {
                base.showMsg('请选择车型', function() {}, '');
            }
            if (localStorage.getItem('OEMARRTRUE')) {
                var oemarr = localStorage.getItem('OEMARRTRUE').split(",");
                //console.log(oemarr)
                if (OEMARR == "") {
                    base.showMsg('请选择oem信息', function() {}, '');
                } else if (OEMARRTrue == "" && oemarr == "") {
                    base.showMsg('请先加入询价单', function() {}, '');
                } else {
                    selsctfunction.inquirySheetfn(oemarr, OEMARRTrue);
                    var secondid;
                    localStorage.setItem('OEMARRTRUE', OEMARRTrue);
                    $state.go("main.inquirySheet", { threeId: currentt, twoId: selsctfunction.Sheetfn($scope.parentid, secondid), oneId: current, partsId: partsId })
                }
            } else {
                if (OEMARR == "" && oemarr == "") {
                    base.showMsg('请选择oem信息', function() {}, '');
                } else if (OEMARRTrue == "") {
                    base.showMsg('请先加入询价单', function() {}, '');
                } else {
                    var secondid;
                    localStorage.setItem('OEMARRTRUE', OEMARRTrue);
                    $state.go("main.inquirySheet", { threeId: currentt, twoId: selsctfunction.Sheetfn($scope.parentid, secondid), oneId: current, partsId: partsId })
                }
            }
        }

        //根据车型搜索oem表
        $scope.search = function(currentt) {
            $rootScope.spinner = true;
            if ($('#partsId').val()) {
                partsId = $('#partsId').val();
                //  console.log(partsId)
            } else if ($location.search().txtoemId) {
                partsId = "";
            } else {
                base.showMsg('请选择车型', function() {}, '');
            }
            var threeClassifyId = currentt; //三级分类id
            var frontOrBehind;
            var leftOrRight;
            var mainOrAttach;
            var oemCoadeTrue; //oemcode
            if ($("#txtOEM").val() != "") {
                oemCoadeTrue = $("#txtOEM").val();
                partsId = "";
            } else {
                oemCoadeTrue = "";

            }

            //判断oem和三级分类ID是否存在
            if (oemCoadeTrue == "" && threeClassifyId == undefined) {
                base.showMsg('请选择车型或oem', function() {}, '');
            } else {
                var param = {
                    "threeClassifyId": threeClassifyId,
                    "frontOrBehind": selsctfunction.oemload(OEMIDTRUE.get('i'), frontOrBehind, "前", "后", 1, 2),
                    "leftOrRight": selsctfunction.oemload(OEMIDTRUE.gettwo('i'), leftOrRight, "左", "右", 3, 4),
                    "mainOrAttach": selsctfunction.oemload(OEMIDTRUE.getthree('i'), mainOrAttach, "主体件", "安装附件", 5, 6),
                    "partsId": partsId,
                    "oemCode": oemCoadeTrue
                }
                commonService.Attribute(param, function(data) {
                    //console.log(data)
                    if (data.code == 0) {

                        $scope.oemlist = data.data;
                        $scope.ifSelect = function(id) {
                            return OEMARR.indexOf(id) != -1;
                        };
                    }
                })
            }

        }

        //一级分类默认加载及点击
        //      $scope.current=1;
        $scope.TopCategory = function(x,y) {
        	if(y==1){
        		$scope.currenttwo = true;
	            $scope.showTwo = false;
	            $scope.current = x;
        	}
           
        }

        /*oemList点击搜索获取数据*/
        $scope.$on('oemList', function(e, v) {
            $scope.oemlist = v;
            $scope.current = "";
            $scope.currentt = "";
            $scope.showTwo = false;
            //console.log($scope.current);              
        });

        /*oem根据五级分类的下拉框显示最新的oem分类的数据*/
        $scope.$on('changeModel', function(ev, v) {
            $scope.oemlist = v;
        })
		 $scope.$on('changeModelone', function(ev, v) {
            $scope.dataoemcode = v;
        })
        $("#carTypeFive").change(function() {
            $scope.ifSelect = function(id) {
                return OEMARR.indexOf(id) != -1;
            };

        });

        //三级分类加载
        $scope.reclassify = function(x,y) {
        	if(y==1){
        		$rootScope.spinner = true;
//	            console.log($('#partsId').val())
	            if ($('#partsId').val() != "") {
	                partsId = $('#partsId').val();
	                //                  console.log(partsId)
	            } else if ($location.search().txtoemId) {
	                partsId = "";
	            } else {
	                base.showMsg('请选择车型', function() {}, '');
	            }
	            $scope.currentt = x.goodsAssortmentId;
	            $scope.parentid = x.goodsAssortmentParentId;
	            $('#partsIsecondtrue').val(x.goodsAssortmentId);
	
	            var param = {
	                "assortId": x.goodsAssortmentId,
	                "partsId": partsId,
	            }
	            var params = JSON.stringify(param)
	            commonService.LoadList(params, function(data) {
	                if (data.code == 0) {
	
	                    $scope.oemlist = data.data;
	                    $scope.ifSelect = function(id) {
	                        return OEMARR.indexOf(id) != -1;
	                    };
	                }
	            });
        	}
            
        };

        //显示所有二级分类
        $scope.show = function() {
            $scope.currenttwo = true;
            $scope.showTwo = false;
        };

        //清空
        $scope.empty = function(oemlist) {
            OEMARR = [];
            for (var i in oemlist) {
                oemlist[i].dataOEM = false;
            }
            $scope.ifSelect = function() {
                return false;
            };
            $scope.ifSelected = false;
        }

        //console.log($stateParams.txtoemId);
        var selsctfunction = (function() {
            var oemload = function(obj, objtwo, attrone, attrtwo, num, numone) {
                if (obj == attrone) {
                    objtwo = num;
                } else if (obj == attrtwo) {
                    objtwo = numone;
                } else {
                    objtwo = "";
                }
                return objtwo
            };
            var inquirySheetfn = function(OEMARR, OEMARRTrue) {
                for (var i = 0; i < OEMARR.length; i++) {
                    if (OEMARRTrue.indexOf(OEMARR[i]) < 0) {
                        OEMARRTrue.push(OEMARR[i])
                    }
                }
                return OEMARRTrue
            };
            var Sheetfn = function(parentid, secondid) {
                if (parentid == undefined) {
                    secondid = $location.search().twoId;
                } else {
                    secondid = parentid;
                }
                return secondid
            }
            return {
                oemload: oemload,
                inquirySheetfn: inquirySheetfn,
                Sheetfn: Sheetfn
            }
        })();

    }])
})
